package com.fwx.chatroom.controller;

import com.fwx.chatroom.mapper.FriendMapper;
import com.fwx.chatroom.model.Friend;
import com.fwx.chatroom.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class FriendController {
    @Autowired
    private FriendMapper friendMapper;

    /*获取好友列表*/
    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest request) {
        //1.从会话中获取userId
        HttpSession session = request.getSession(false);
        if(session == null) {
            log.info("session不存在！");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            //当前用户对象没在会话中存在
            log.info("用户不在会话中存在！");
            return new ArrayList<Friend>();
        }
        //2.根据userId，从数据库查询数据
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        log.info("friendList: "+friendList);
        return friendList;
    }
}
